import NumberCompareInput from './numberCompareInput.vue'
import NumberRangeSelect from './numberRangeSelect.vue'

import { cloneDeep } from 'lodash-es'
import { useI18n } from '@/locales'
const { t } = useI18n()

const commonConditionType = {
    IS_NULL: t('filterConditionType.IS_NULL'),
    IS_NOT_NULL: t('filterConditionType.IS_NOT_NULL')
}
export const filterConditionTypeMap = {
    TEXT: Object.assign(cloneDeep(commonConditionType), {
        BELONG: t('filterConditionType.BELONG'),
        NOT_BELONG: t('filterConditionType.NOT_BELONG'),
        CONTAIN: t('filterConditionType.CONTAIN'),
        NOT_CONTAIN: t('filterConditionType.NOT_CONTAIN'),
        PRE_IS: t('filterConditionType.PRE_IS'),
        PRE_NOT_IS: t('filterConditionType.PRE_NOT_IS'),
        END_IS: t('filterConditionType.END_IS'),
        END_NOT_IS: t('filterConditionType.END_NOT_IS')
    }),
    DECIMAL: Object.assign(cloneDeep(commonConditionType), {
        BETWEEN: t('filterConditionType.BETWEEN'),
        NOT_BETWEEN: t('filterConditionType.NOT_BETWEEN'),
        EQUALS: t('filterConditionType.EQUALS'),
        NOT_EQUALS: t('filterConditionType.NOT_EQUALS'),
        GT: t('filterConditionType.GT'),
        LT: t('filterConditionType.LT'),
        GE: t('filterConditionType.GE'),
        LE: t('filterConditionType.LE')
        /*
         * 数据过滤中前N个和后N个,需要后台接口实现,目前是前端计算,所以不需要这2种情况,暂时注释
         * 'TOP_N': t('filterConditionType.TOP_N'),
         * 'LITTLE_N': t('filterConditionType.LITTLE_N')
         */
    }),
    TIMESTAMP: Object.assign(cloneDeep(commonConditionType), {
        BETWEEN: t('filterConditionType.BELONG'),
        NOT_BETWEEN: t('filterConditionType.NOT_BELONG'),
        LT: t('filterConditionType.DATE_LT'),
        GT: t('filterConditionType.DATE_GT'),
        EQUALS: t('filterConditionType.EQUALS'),
        NOT_EQUALS: t('filterConditionType.NOT_EQUALS')
        /*
         * 数据过滤中前N个和后N个,需要后台接口实现,目前是前端计算,所以不需要这2种情况,暂时注释
         * 'TOP_N': t('filterConditionType.DATE_TOP_N'),
         * 'LITTLE_N': t('filterConditionType.DATE_LITTLE_N')
         */
    })
}

export const optionComponentMapping = {
    'TEXT|BELONG': 'ColumnDataSelect',
    'TEXT|NOT_BELONG': 'ColumnDataSelect',
    'TEXT|CONTAIN': 'TextInput',
    'TEXT|NOT_CONTAIN': 'TextInput',
    'TEXT|IS_NULL': '',
    'TEXT|IS_NOT_NULL': '',
    'TEXT|PRE_IS': 'TextInput',
    'TEXT|PRE_NOT_IS': 'TextInput',
    'TEXT|END_IS': 'TextInput',
    'TEXT|END_NOT_IS': 'TextInput',
    'DECIMAL|BETWEEN': 'NumberRangeSelect',
    'DECIMAL|NOT_BETWEEN': 'NumberRangeSelect',
    'DECIMAL|EQUALS': 'NumberInput',
    'DECIMAL|NOT_EQUALS': 'NumberInput',
    'DECIMAL|GT': 'NumberCompareInput',
    'DECIMAL|LT': 'NumberCompareInput',
    'DECIMAL|GE': 'NumberCompareInput',
    'DECIMAL|LE': 'NumberCompareInput',
    'DECIMAL|TOP_N': 'NInput',
    'DECIMAL|LITTLE_N': 'NInput',
    'DECIMAL|IS_NULL': '',
    'DECIMAL|IS_NOT_NULL': '',
    'TIMESTAMP|BETWEEN': 'DateRangeSelect',
    'TIMESTAMP|NOT_BETWEEN': 'DateRangeSelect',
    'TIMESTAMP|LT': 'DateSelect',
    'TIMESTAMP|GT': 'DateSelect',
    'TIMESTAMP|EQUALS': 'DateSelect',
    'TIMESTAMP|NOT_EQUALS': 'DateSelect',
    'TIMESTAMP|TOP_N': 'NInput',
    'TIMESTAMP|LITTLE_N': 'NInput',
    'TIMESTAMP|IS_NULL': '',
    'TIMESTAMP|IS_NOT_NULL': ''
}
export const componentMap = {
    ColumnDataSelect: {
        // TODO 依赖的组件，后期加
        // componentValue: ColumnDataSelect,
        defaultValue: [],
        props: {},
        validate: 'require'
    },
    NumberCompareInput: {
        componentValue: NumberCompareInput,
        defaultValue: {
            type: 'FIXED',
            value: null
        },
        props: {},
        validate: 'custom'
    },
    TextInput: {
        componentValue: 'AInput',
        defaultValue: '',
        props: {},
        validate: 'require'
    },
    NumberRangeSelect: {
        componentValue: NumberRangeSelect,
        defaultValue: {
            min: { type: 'GT', value: undefined },
            max: { type: 'LT', value: undefined }
        },
        props: {},
        validate: 'custom'
    },
    NumberInput: {
        componentValue: 'AInputNumber',
        defaultValue: undefined,
        props: {},
        validate: 'require'
    },
    NInput: {
        componentValue: 'AInputNumber',
        defaultValue: undefined,
        props: { step: 1, min: 0, precision: 0 },
        validate: 'require',
        label: 'N='
    },
    DateSelect: {
        componentValue: 'ADatePicker',
        defaultValue: undefined,
        props: { format: 'YYYY-MM-DD', valueFormat: 'YYYY-MM-DD' },
        validate: 'require'
    },
    DateRangeSelect: {
        // componentValue: DateRangeSelect,
        defaultValue: {
            min: { type: 'GE', value: undefined },
            max: { type: 'LE', value: undefined }
        },
        props: {},
        validate: 'custom'
    }
}

/**
 * 将原始数据的字段类型转换成ITD的字段类型
 * @param {String} type 原始数据的字段类型
 * @returns {String} itd的数据类型
 */
export function convertRawDataTypeToItdType(type) {
    let newType = ''
    switch (type) {
        case 'VARCHAR':
        case 'TEXT':
        case 'STRING':
        case 'ARRAY':
            newType = 'TEXT'
            break
        case 'BIGINT':
        case 'DECIMAL':
        case 'NUMBER':
        case 'INT':
        case 'LONG':
        case 'FLOAT':
        case 'DOUBLE':
        case 'INTEGER':
            newType = 'DECIMAL'
            break
        case 'DATE':
        case 'TIMESTAMP':
            newType = 'TIMESTAMP'
            break
        default:
            newType = ''
    }
    return newType
}

/**
 * 根据数据类型和条件类型获取表单组件
 * @param {*} type 字段类型
 * @param {*} conditionType 条件类型
 * @returns
 */
export function getComponent(type, conditionType) {
    const itdType = convertRawDataTypeToItdType(type)
    const componentKey = optionComponentMapping[`${itdType}|${conditionType}`]
    return componentMap[componentKey]
}

/**
 * 根据数据类型和条件类型获取表单组件默认值
 * @param {*} type 字段类型
 * @param {*} conditionType 条件类型
 * @returns
 */
export function getComponentDefaultValue(type, conditionType) {
    const formComponent = getComponent(type, conditionType)
    if (formComponent) {
        return cloneDeep(formComponent.defaultValue)
    }
}

// 校验过滤条件树
export function handleCheckFilterConditionTree(conditionTree) {
    const { children, expression } = conditionTree

    // 如果存在子节点，则递归校验子节点
    if (children && children.length) {
        // 如果子节点中存在校验失败的条件，则直接返回false
        for (const treeNode of children) {
            const result = handleCheckFilterConditionTree(treeNode)
            if (!result) {
                return result
            }
        }
    } else {
        return handleCheckExpression(expression)
    }
    return true
}

// 校验过滤条件树节点中的表达式
function handleCheckExpression(expression) {
    const { filterType, filterCondition } = expression || {}

    // 如果不是合法的表达式，则校验失败
    if (!filterType || !filterCondition) {
        return false
    }

    // 校验条件过滤节点的表达式
    if (filterType === 'CONDITION') {
        const { filterColumnName, conditionType, value } = filterCondition

        if (filterColumnName && conditionType) {
            // 如果当前的条件类型需要value，则校验value是否为空
            const hasValue = value || value === 0
            if (['IS_NULL', 'IS_NOT_NULL'].includes(conditionType) || hasValue) {
                return true
            }
        }

        return false
    }

    // 校验公式过滤节点中的表达式
    if (filterType === 'FORMULA') {
        const { formula } = filterCondition
        return Boolean(formula)
    }

    return false
}
